package com.windea.study.kotlin.game.tankwar2.model

import com.windea.study.kotlin.game.tankwar2.*
import com.windea.study.kotlin.game.tankwar2.enums.*
import com.windea.study.kotlin.game.tankwar2.interfaces.*

/**坦克。*/
abstract class Tank(
	override var x: Int,
	override var y: Int
) : Sufferable, Movable, Blockable, Destroyable {
	override val width: Int = GameConfig.block
	override val height: Int = GameConfig.block
	
	/**不可以走的方向。*/
	protected open var badDirection: Direction? = null
	
	override fun onCollision(other: Blockable?, direction: Direction?, entities: MutableList<Entity>) {
		badDirection = direction
	}
	
	override fun onSuffer(other: Attackable, entities: MutableList<Entity>) {
		health -= other.attack
		entities += Blast(x, y)
	}
	
	override fun isDestroyed(): Boolean {
		return health <= 0
	}
	
	open fun move(nextDirection: Direction, entities: MutableList<Entity>) {
		//坦克的坐标需要发生变化，根据不同的方向，改变对应的坐标
		when(direction) {
			Direction.Up -> y -= speed
			Direction.Down -> y += speed
			Direction.Left -> x -= speed
			Direction.Right -> x += speed
		}
		
		//越界判断
		if(x < 0) x = 0
		if(x > GameConfig.width - width) x = GameConfig.width - width
		if(y < 0) y = 0
		if(y > GameConfig.height - height) y = GameConfig.height - height
	}
}
